// pages/inferioraList/inferioraList.js
const app = getApp()
import {
  checkInfoList
} from '../../api/dealer'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    index: 1,
    isFinish: false,
    loadingMoreHidden: false,
    goodsId: '',
    canOpen: 0,
    touchStartX: 0,
    CustId: '',
    title: '',
    dealerInfo: null,
    searchValue: '',
    teamTitle: '',
    r0: 0,
    r1: 0,
    r2: 0,
    r3: 0,
    r4: 0,
    r5: 0,
    r7: 0,
    r8: 0,
    show: false,
    isShowBuyDetail: false,
    orderList: [],
    orderIndex: 1,
    orderIsFinish: false,
    orderIsLoading: false,
    currentCustId: 0
  },
  close() {
    this.setData({
      show: false
    })
  },
  contact(e) {
    console.log(e);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
    })
  },
  touchStart(e) {
    //设置初始位置的值为滑动的坐标
    if (!this.data.canOpen) {
      this.setData({
        touchStartX: e.changedTouches[0].clientX
      })
    }
  },
  changeValue(e) {
    console.log(e);
    if (e.detail.value == "") {
      this.clear();
    }
    this.setData({
      searchValue: e.detail.value
    })
  },
  clear() {
    this.setData({
      list: [],
      index: 1,
      isFinish: false,
      searchValue: ''
    })
    this.getData()
  },
  getDetail(e) {
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;

    this.setData({
      teamTitle: title
    })
    // console.log(e.currentTarget);
    if (this.data.isShowBuyDetail) {
      // app.get(`Finance/get_sum_batch_statistics?CustId=${id}`,res=>{
      //   if(res&&res.code=='0'){
      //     this.setData({
      //       r7:res.result.r1,
      //       r8:res.result.r2
      //     })
      //   }
      // });
    }
    // app.get(`stat/get_sum_t_order_goods_statistics?CustId=${id}`,res=>{
    //   console.log('goods',res);
    //   if(res.code=='0'){
    //     this.setData({
    //       goods:res.result
    //     })
    //   }
    // })
    // app.get(`/stock/get_sum_t_stock_statistics?CustId=${id}`,res=>{
    //   if(res&&res.code=='0'){        
    //     this.setData({
    //       r0:res.result.r0,
    //       r1:res.result.r1,
    //       r2:res.result.r2,
    //       r3:res.result.r3,
    //     })
    //     // console.log(this.data);
    //   }
    // })
    // app.get('/Finance/get_user_rebate_stat?CustId='+id, res => {
    //   console.log(res)
    //   if(res&&res.code=='0'){     
    //       let score=res.result;          
    //       this.setData({
    //         r4: score.B?score.B:0,
    //         r5: score.A?score.A:0,
    //         r6: Math.floor((score.A?score.A:0)*0.8)
    //       })
    //      }
    //     // console.log(this.data);
    // })
    this.setData({
      position: 'center',
      show: true,
      currentCustId: id,
      orderIndex: 1,
      orderIsFinish: false,
      orderList: [],
    })
    // this.getOrders();
  },
  search() {
    wx.showLoading()
    app.get(`dealer/get_dealers?IsValid=all&Name=${this.data.searchValue}&FromId=${app.globalData.uid}&size=100000`, res => {
      if (res.count == 0) {
        wx.hideLoading()
        wx.showToast({
          title: '暂无该经销商，请核对后查询',
          icon: 'none'
        })
        return
      } else {
        wx.hideLoading()
        this.setData({
          isFinish: true,
          list: res.result.map(item => {
            item.CreateTime = item.CreateTime.substr(0, 10)
            item.show = false
            return item;
          })
        })
      }
    })
  },
  goToChat(e) {
    let id = e.currentTarget.dataset.id1
    let toId = e.currentTarget.dataset.toid
    console.log(e)
    let title = `${e.currentTarget.dataset.title1}`
    console.log(id, title)
    // wx.navigateTo({
    //   url: `../im/im?id=${id}&title=${title}&isGroup=0&toid=${toId}`,
    // })
  },
  touchEnd(e) {
    if (!this.data.canOpen) {
      //获取当前点击index
      const index = e.currentTarget.dataset.index
      //重新赋值
      const clockList = this.data.list
      //判断初始值是否大于移动之后的值+50 如果大于判断是左划删除 删除按钮显示
      if (this.data.touchStartX > e.changedTouches[0].clientX + 50) {
        console.log(clockList[index])
        clockList[index].show = true
      }
      //判断初始值是否小于等于初始值 如果小于判断是用户右滑 删除按钮隐藏
      if (this.data.touchStartX <= e.changedTouches[0].clientX) {
        clockList[index].show = false
      }
      //更新clockList
      this.setData({
        list: clockList
      })
    }
  },
  checkHisTeam(e) {
    //将index值作为参数传入,必须是index数组下标
    let uid = e.currentTarget.dataset.id
    let title = e.currentTarget.dataset.title
    let face = e.currentTarget.dataset.face
    if (this.data.canOpen == 1) {
      wx.navigateTo({
        url: `./address?uid=${ uid }`
      })
    } else {
      if (e.currentTarget.dataset.count < 1) {
        wx.showToast({
          title: '该用户暂无团队',
          icon: 'none'
        })
        return
      }
      wx.navigateTo({
        url: `./inferioraList?id=${ uid }&title=${ title }&face=${face}`
      })
    }

  },
  changeAddress(e) {
    let uid = e.currentTarget.dataset.id
    let title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: `./inferioraOrder?uid=${ uid }&title=${ title }`
    })
  },

  getData() {
    let that = this
    if (that.data.isFinish) {
      wx.hideLoading()
      return
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    checkInfoList(that.data.custId, that.data.index, wx.getStorageSync('access_token')).then(res => {
      wx.hideLoading()
      if (res && res.code == 10000) {
        that.setData({
          dealerInfo: res.data.superior
        })
        if (res.data.children.records.length <= 0) {
          that.setData({
            isFinish: true
          })
          return
        }
        that.setData({
          list: that.data.list.concat(res.data.children.records),
        })
        that.setData({
          loadingMoreHidden: that.data.list.length > 0
        })
        that.data.index = that.data.index + 1
      } else {
        that.setData({
          isFinish: true
        })
      }
    })

  },

  getOrders() {
    let _this = this;
    if (this.data.orderIsFinish) {
      return
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    app.get(`order/get_orders?CustId=${this.data.currentCustId}&Status=unfinish&page=${this.data.orderIndex}&size=20`, (res) => {
      wx.hideLoading();
      if (res && res.code == "0") {
        if (res.result.length > 0) {
          _this.setData({
            orderList: _this.data.orderList.concat(res.result),
            orderIndex: this.data.orderIndex + 1
          });
        } else {
          _this.setData({
            orderIsFinish: true,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options.title) {
      that.setData({
        custId: options.id,
        title: options.title + '的团队',
        topHeader: options.face
      })
    } else {
      console.log(app.globalData.dealerInfo)
      that.setData({
        custId: app.globalData.dealerInfo.id,
        title: '我的团队',
        topHeader: app.globalData.userInfo.face
      })
    }
    this.getData()
    wx.hideShareMenu()
    this.data.canOpen = options.canOpen;
    this.setData({
      goodsId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      isShowBuyDetail: app.globalData.uid == 46
    })
    // app.get(`dealer/get_from_dealer_by_custid?CustId=${app.globalData.uid}`, res => {      
    //   if(res.result){
    //     res.result.CreateTime = res.result.CreateTime.substr(0, 10)
    //     this.setData({
    //       dealerInfo: res.result
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})